<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\User;
use App\Models\GoodsNexus;

class TestController extends Controller {

    public function clearOrder(Request $request) {
        $user = User::where('telphone', $request->input('telphone'))->first();
        if (!$user) {
            return '用户不存在';
        }
        $order = Order::where('user_id', $user->id)->first();
        if (!$order) {
            return '该用户没有订单';
        }
        $rfids = OrderDetail::where('order_id', $order->id)->pluck('rfid')->toArray();
        //还原rfid
        GoodsNexus::whereIn('rfid', $rfids)->update(['state' => 0]);
        //删除订单详情
        OrderDetail::where('order_id', $order->id)->delete();
        //删除订单
        Order::where('user_id', $user->id)->delete();
        return $user->name . '的订单清除成功';
    }

    public function deleteOrder(Request $request) {
        $order = Order::where('order_num', $request->input('order_num'))->first();
        if (!$order) {
            return '订单不存在';
        }
        $orderDetail = OrderDetail::where('order_id', $order->id)->get();
        $rfids = $orderDetail->pluck('rfid')->toArray();
        GoodsNexus::whereIn('rfid',$rfids)->update(['state'=>0]);
        OrderDetail::destroy($orderDetail->pluck('id')->toArray());
        Order::destroy($order->id);
        return '删除成功';
    }

}
